#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename:   logServer-messageFile.sh
# Version:    1.0
# Date:       2021/03/30
# Author:     laiyixin
# Email:      laiyixin@kylinos.com.cn
# History：
#             Version 1.0, 2021/03/30
# Function:   logServer-messageFile - 系统日志文件存放路径
# Out:
#              0=> Success
#              1=> Fail
#              other=> TCONF
# ----------------------------------------------------------------------

## TODO: 使用ctrl+c退出
##
logServerOnCtrlC() {
    echo "正在优雅的退出..."
    logServerClean

    exit ${TFAIL}
}

## TODO: 用户界面
##
logServerUSAGE() {
    cat >&1 <<EOF
--------- logServer-messageFile - 系统日志文件存放路径 ---------
EOF
}

## TODO : 执行前的初始化
## Out  :
##        0=> Success
##        1=> Fail
##        other=> TCONF
logServerInit() {
    # 判断root用户
    if [ $(id -u) -ne 0 ]; then
        echo "Must use root ！"
        exit ${TCONF}
    fi

    # 信号捕获ctrl+c
    trap 'logServerOnCtrlC' INT

    logServerClean

}

## TODO : 执行进行时的方法
## Out  :
##        0=> Success
##        1=> Fail
##        other=> TCONF
logServerTest() {
    if ! grep -E '^\s*[^#].*/var/log/messages' /etc/rsyslog.conf; then
        echo '*.info;mail.none;authpriv.none;cron.none                /var/log/messages' >>/etc/rsyslog.conf
        logServerRetParse "rsyslog add /var/log/messages /etc/rsyslog.conf"
    fi
}

## TODO : 执行收尾清除工作
##
logServerClean() {
    echo "Clean ..."

}

## TODO: 解析函数返回值
## In  : $1 => log
logServerRetParse() {
    local ret=$?
    local logstr=""

    if [ $# -eq 1 ]; then
        logstr="$1"
    fi

    if [ $ret -eq 0 ]; then
        echo "[pass] : ${logstr}"
    else
        echo "[fail] : ${logstr}"
        logServerClean
        exit ${TFAIL}
    fi
}

## TODO : Main
## Out  :
##        0=> Success
##        1=> Fail
##        other=> TCONF
logServerMain() {
    logServerUSAGE

    logServerInit

    logServerTest

    logServerClean

    return ${TPASS}
}

logServerMain
exit $?
